Rock, paper, scissors

您所在的位置:网站首页 scissors stone Rock, paper, scissors

Rock, paper, scissors

2024-06-10 00:21| 来源: 网络整理| 查看: 265

Step 1: Make itWhat is it?

Play this classic game with two micro:bits and learn about selection, variables and random numbers at the same time.

These two videos show you what you'll make and how to code it:

IntroductionCoding guideHow it worksRock, paper, scissors is a classic game of chance for two people. You and a partner shake your fists 3 times and then make gestures at random to show a rock, paper or scissors. Rock beats scissors, scissors beat paper and paper beats rock (it wraps the rock!).When the micro:bit accelerometer detects a shake movement, it sets the variable tool to a random number: 0, 1 or 2.We use 0 because computers start counting at 0, and it’s good to remember that 0 is a number!The program uses selection to decide what image to show on the LED display. If the random number was 0, it shows a rock icon, if it was 1 it shows the icon representing paper. If it wasn’t 0 or 1, it must be 2 because we instructed the micro:bit to only pick random numbers between 0 and 2, so in that case it shows scissors.What you need2 micro:bits (or MakeCode simulator)MakeCode or Python editorbattery packs (optional)a partner to play withStep 2: Code it

Loading...

Open in ClassroomOpen in MakeCodeDownload HEX1from microbit import * 2import random 3 4while True: 5 if accelerometer.was_gesture('shake'): 6 tool = random.randint(0,2) 7 if tool == 0: 8 display.show(Image.SQUARE_SMALL) 9 elif tool == 1: 10 display.show(Image.SQUARE) 11 else: 12 display.show(Image.SCISSORS)Open in ClassroomOpen in PythonDownload HEXStep 3: Improve itDraw your own icons for rock, paper, scissors.Think of other tools that could replace rock, paper and scissors or invent new rules.Use the micro:bit radio function to make a game that knows if you won or lost by communicating with your friend’s micro:bit.


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3